﻿using System;
using System.Reflection;
using Unity;
using XQ.Net.Core.Export;
using XQ.Net.SDK.Attributes;

namespace XQ.Net.Core
{
    internal class PluginInitializer
    {
        /// <summary>
        /// 允许使用接口继承
        /// </summary>
        /// <param name="unityContainer"></param>
        public static void Register(IUnityContainer unityContainer)
        {
        }

        /// <summary>
        /// 初始化插件本体程序集
        /// </summary>
        /// <param name="assembly"></param>
        internal static void Init(Assembly assembly)
        {
            try
            {
                _Main.Log("Reading...");

                foreach (var item in assembly.GetTypes())
                {
                    if (item.GetCustomAttribute<PluginAttribute>() != null)
                    {
                        //插件事件类
                        foreach (var met in item.GetMethods())
                        {
                            foreach (var attr in met.GetCustomAttributes(true))
                            {
                                if (attr as GroupMsgEventAttribute != null)//群聊事件
                                {
                                    _Event.Event_GroupMsgHandler += (s, e) => met.Invoke(null, new object[] { s, e });
                                }
                                if (attr as PrivateMsgEventAttribute != null)//私聊事件
                                {
                                    _Event.Event_PrivateMsgHandler += (s, e) => met.Invoke(null, new object[] { s, e });
                                }
                                if (attr as EnableEventAttribute != null)//启动事件
                                {
                                    _Event.Event_AppEnableHandler += (s, e) => met.Invoke(null, new object[] { s, e });
                                }
                                if (attr as DisableEventAttribute != null)//停止事件
                                {
                                    _Event.Event_AppDisableHandler += (s, e) => met.Invoke(null, new object[] { s, e });
                                }

                                if (attr as AddGroupAttribute != null)//加群/邀请进群/主动进群事件
                                {
                                    _Event.Event_AddGroupHandler += (s, e) => met.Invoke(null, new object[] { s, e });
                                }
                                if (attr as AddFriendAttribute != null)//加好友事件
                                {
                                    _Event.Event_AddFriendHandler += (s, e) => met.Invoke(null, new object[] { s, e });
                                }
                                if (attr as UnBanSpeakAttribute != null)//解除禁言
                                {
                                    _Event.Event_UnBanSpeak += (s, e) => met.Invoke(null, new object[] { s, e });
                                }
                                if (attr as BanSpeakAttribute != null)//禁言
                                {
                                    _Event.Event_BanSpeak += (s, e) => met.Invoke(null, new object[] { s, e });
                                }
                                if (attr as GroupNameChanged != null)//禁言
                                {
                                    _Event.Event_GroupNameChanged += (s, e) => met.Invoke(null, new object[] { s, e });
                                }
                                if (attr as CallMenuAttribute != null)//Menu
                                {
                                    _Event.Event_CallMenu += (s, e) => met.Invoke(null, new object[] { s, e });
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _Main.Log(ex.ToString());
            }
        }
    }
}